/*
整合请求数据
 */
import http from './http';
// import { SessionStorage } from '@/utils/storage';
import qs from 'qs';
import _ from 'lodash';
export default {
  accessAPI: function (opts) {
    const apiObj = opts.apiObj || {};
    const _method = apiObj.method;
    let _body = {};
    const _path = opts.path;
    const _query = {};
    const _headers = {};
    let apiUrl = apiObj.url;
    if (_.isPlainObject(_path)) {
      Object.keys(_path).forEach(key => {
        apiUrl = apiUrl.replace(':' + key, _path[key]);
      });
    }
    // 清一下空属性
    // if (opts.body && !_.isEmpty(opts.body) && !Array.isArray(opts.body)) {
    //   Object.keys(opts.body).forEach(key => {
    //     if (opts.body[key] !== '' && opts.body[key] !== undefined && opts.body[key] !== null) {
    //       _body[key] = opts.body[key];
    //     }
    //   });
    // } else {
    //   _body = opts.body;
    // }
    _body = opts.body;
    if (opts.query && !_.isEmpty(opts.query)) {
      Object.keys(opts.query).forEach(key => {
        // if (opts.query[key] !== '' && opts.query[key] !== undefined && opts.query[key] !== null) {
        _query[key] = opts.query[key];
        // }
      });
    }
    return http({
      url: apiUrl,
      method: _method,
      params: _method.toUpperCase() === 'POST' ? _query : _body,
      data: _method.toUpperCase() === 'POST' ? qs.stringify(_body) : _body,
      headers: _headers
    });
  }
};
